--#include "data\config\title\HeadTitleItemConfig.lua" once
--#include "data\config\title\HeadTitle.lua" once

function UseHeadTitleItem( sysarg, itemidx, itemPtr, ItemTable ) 
  local headtitleid = tonumber(ItemTable.headtitle_id )

local cfg
  if headtitleid ~= 0 then
    for idx = 1, #HeadTitle do
      if HeadTitle[idx].titleid == headtitleid then
        cfg = HeadTitle[idx]
        break
      end
    end
  end

  if(cfg == nil)then
    return
  end
  if (Actor.hasTopTitle( sysarg, headtitleid ) and cfg.titleTime <=0 and cfg.overlayCount <= 0) then
    Actor.sendTipmsg(sysarg, Lang.ScriptTips.headtitle0001, ttFlyTip)
    return
  end
  if cfg.overlayCount > 0 and Actor.getTopTitleOverlayCount(sysarg, cfg.titleid) >= cfg.overlayCount then
     Actor.sendTipmsg(sysarg, Lang.ScriptTips.headtitle0003, ttFlyTip)
     return
  end
    if Actor.removeItemByPtr(sysarg,itemPtr,1,true,Lang.LogTips.HeadTitleItemUse,LogIds.UseHeadTitleItemLogId) > 0 then 
        local strNotice = string.format(Lang.ScriptTips.headtitle0002,cfg.titleName)
        Actor.sendTipmsg(sysarg, strNotice , ttFlyTip)
        Actor.addTopTitle( sysarg, headtitleid )
        Actor.collectOperate(sysarg, 0)
        return true
    end
end

function UseHeadTitleItemInit(sysarg)
	for k, v in ipairs(HeadTitleItemConfig) do
		GlobalItemFn[v.item_id] = { func = UseHeadTitleItem, params = v }
	end
end

table.insert(InitFnTable, UseHeadTitleItemInit)
